<?php
defined('_JEXEC') or die('Direct Access to ' . basename(__FILE__) . ' is not allowed.');

$session=JFactory::getSession();
$tmp=$session->get('regs5.data');
$group=269+$tmp['no'];
$coupon=$tmp['cp'];
$total=$tmp['total_nam'];
$db = JFactory::getDBO();
$db->setQuery("SELECT * FROM #__coupon_");
$res=$db->loadObjectList();

if($coupon && $res){
	date_default_timezone_set("Europe/Copenhagen");
	foreach($res as $row){
		if($row->value == $coupon && $row->state){
			$m1='';$m2='';
			switch($row->type){
				/*case 0:
					$m1="SET state=0";
					$m2="description=".$db->quote(date("d.m.Y H:i"));
					break;*/
				case 0:
					$row->description+=1;
					$row->description.=" time(s) used | ".date("d.m.Y H:i");
					$m2="description=".$db->quote($row->description);
					break;
			}
			$total-=$row->price;
			//update session
			$tmp['total_nam']=$total;
			$tmp['cp']=$row->price;
			$session->set('regs5.data',$tmp);
			$db->setQuery("UPDATE #__coupon_
			".$m1.($m1 ? "," : "SET ").$m2."
			WHERE id=".$row->id);
			$res=$db->query();
			break;
		}
	}
}

file_put_contents('tpm.idx', $total);

function ConfirmedOrder1($total,$group) {

	$db = JFactory::getDBO();
	$sess=JFactory::getSession();
	
	$orderno=time();
	$sess->set("regs5.on", $orderno);

	switch($group){
		case 270:
			$desc='Small';break;
		case 271:
			$desc='Medium';break;
		case 272:
			$desc='Large';break;
		case 273:
			$group=310;
			$desc='Fitness';break;
		
		default:
			$desc='false';
			break;
	}

	$post_variables = Array(
		'protocol' => '7',
		'msgtype' => 'subscribe',
		'merchant' => "33760310",
		'language' => "da",
		'ordernumber' => $orderno,
		'amount' => $total*100,
		'currency' => "DKK",
		'continueurl' => JROUTE::_(JURI::root() . "index.php?option=com_users&task=registration.register"),
		'cancelurl' => JROUTE::_(JURI::root() . "index.php?option=com_users&view=registration&layout=cancel"),
		'callbackurl' => JROUTE::_(JURI::root() . "index.php?option=com_users&view=registration&layout=callback"),
		'description' => $desc,
		'group' => $group,
		'forcemobile' => "1"
	);

	// Calculate md5
	$md5String = '';
	foreach ($post_variables as $v)
		$md5String .= $v;

	$md5String .= "9ead3e386702116bc6aec0819a668e0d0902a6e728e85fe16002d591f37564d8";//quickpay_md5_key
	$post_variables['md5check'] = md5($md5String);

	// add form data
	$html = '<div class="wrapper"><div class="content" id="how-page"><div class="col-wrapper clearfix col-box" style="min-height:300px"><div class="f-login clearfix">
	<form action="https://secure.quickpay.dk/form/" method="post" name="vm_quickpay_form" >';
	$html.= '<input type="submit" value="Click to pay with Quickpay" />';
	foreach ($post_variables as $name => $value) {
	$html.= '<input type="hidden" name="' . $name . '" value="' . htmlspecialchars($value) . '" />';
	}
	$html.= '</form>
	</div>
		</div>
	</div>
</div>';

	$html.= '<script type="text/javascript">';
	$html.= 'document.vm_quickpay_form.submit();';
	$html.= '</script>';
	//send email and redirect?
	echo $html;
}
ConfirmedOrder1($total,$group);